package scct.jiangjiale.ui;

import java.awt.event.*;
import scct.jiangjiale.dao.UserDao;
import scct.jiangjiale.model.User;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/*
 * Created by JFormDesigner on Thu Jun 27 08:25:52 CST 2024
 */



/**
 * @author 蒋家乐
 */
public class panelUserManager extends JPanel {
    DefaultTableModel model;
    FormMain main;
    public panelUserManager(FormMain main) {
        this.main=main;
        initComponents();
        initView();
    }
    private void initView() {
        //获取表格对象的数据模型，同对模型的操作，可以为表格添加和现实数据
         model =(DefaultTableModel) table1.getModel();
        model.addColumn("id");
        model.addColumn("用户名");
        model.addColumn("密码");
        model.addColumn("性别");
        model.addColumn("年龄");
        model.addColumn("手机号码");
        getTable();
    }
    private void getTable() {
        model.setRowCount(0);
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        for (User user : allUser) {
            model.addRow(new Object[]{user.getId(), user.getName(), user.getPassword(), user.getSex(), user.getAge()});
        }
    }

    private void button4(ActionEvent e) {
        DigUserAdd digUserAdd = new DigUserAdd(main);
        //设置模态对话框
        digUserAdd.setModal(true);
        digUserAdd.setVisible(true);
        getTable();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 蒋家乐
        panel2 = new JPanel();
        button4 = new JButton();
        button3 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing. border
        .EmptyBorder ( 0, 0 ,0 , 0) ,  "JFor\u006dDesi\u0067ner \u0045valu\u0061tion" , javax. swing .border . TitledBorder. CENTER ,javax
        . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog", java .awt . Font. BOLD ,
        12 ) ,java . awt. Color .red ) , getBorder () ) );  addPropertyChangeListener( new java. beans
        .PropertyChangeListener ( ){ @Override public void propertyChange (java . beans. PropertyChangeEvent e) { if( "bord\u0065r" .equals ( e.
        getPropertyName () ) )throw new RuntimeException( ) ;} } );
        setLayout(new BorderLayout());

        //======== panel2 ========
        {
            panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- button4 ----
            button4.setText("\u65b0\u589e");
            button4.addActionListener(e -> button4(e));
            panel2.add(button4);

            //---- button3 ----
            button3.setText("\u4fee\u6539");
            panel2.add(button3);
        }
        add(panel2, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 蒋家乐
    private JPanel panel2;
    private JButton button4;
    private JButton button3;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
